//
//  LKDoubuleBarView.m
//  YLHelper
//
//  Created by she1323 on 2018/5/18.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKDoubuleBarView.h"
#define SpotLabelNumber 5
@implementation LKDoubuleBarView

+(instancetype)creteDoubleViewWithFrame:(CGRect)frame andLineValues:(NSArray *)lineValues andBarValues:(NSArray *)barValues andXvalue:(NSArray *)xvalues AndShowRightAxis:(BOOL)showRight AndLinesClors:(NSArray *)lineColors AndBarColors:(NSArray *)barColors AndLineNames:(NSArray *)lineNames AndBarNames:(NSArray *)barNames{
    float groupSpace = 0.5f;
    float barSpace = 0.00f;
    float barWidth = 0.25f;
    LKDoubuleBarView *view = [[LKDoubuleBarView alloc] initWithFrame:frame];
    view.xValusArray = xvalues;
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, view.bounds.size.width, 50)];
    [view addSubview:topView];
    for (NSInteger i = 0; i < (lineNames.count + barNames.count); i++) {
        UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(i *(topView.bounds.size.width / SpotLabelNumber), 0, topView.bounds.size.width / SpotLabelNumber, 50)];
        [topView addSubview:subView];
        UIView *spotView = [[UIView alloc] init];
        if (barColors.count - 1 < i) {
            spotView.backgroundColor = lineColors[i - barColors.count];
        }else{
            spotView.backgroundColor = barColors[i];
        }
        
        [subView addSubview:spotView];
        [spotView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(10, 10));
            make.centerX.equalTo(subView);
            make.centerY.equalTo(subView.mas_centerY).offset(-8);
        }];
        spotView.layer.cornerRadius = 5;
        spotView.layer.masksToBounds = YES;
        UILabel *titeLabel = [[UILabel alloc] init];
        titeLabel.textAlignment = NSTextAlignmentCenter;
        if (barColors.count - 1 < i) {
            titeLabel.text = lineNames[i - barColors.count];
        }else{
            titeLabel.text = barNames[i];
        }
        titeLabel.font = [UIFont systemFontOfSize:10];
        titeLabel.textColor = COLOR_HEX(0x3B3B3C);
        [subView addSubview:titeLabel];
        [titeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(subView);
            make.top.equalTo(spotView.mas_bottom).offset(6);
        }];
        
    }
    CombinedChartView * combineChartView1 = [[CombinedChartView alloc] initWithFrame:CGRectMake(0, 50, view.bounds.size.width, view.bounds.size.height - 50)];
    combineChartView1.delegate = view;
    combineChartView1.noDataText = @"暂无数据";
    combineChartView1.chartDescription.text = @"";
    [view addSubview:combineChartView1];
    
    NSMutableArray *alllineValues = [[NSMutableArray alloc] init];
    double rightSetMin = 0;
    double rightSetMax = 0;
    for (NSInteger i = 0; i < lineValues.count; i++) {
        NSArray *linearray = lineValues[i];
        NSMutableArray *lineValls = [[NSMutableArray alloc] init];
        for (int j = 0; j < linearray.count; j++)
        {
            NSString *valStr = [NSString stringWithFormat:@"%@", linearray[j]];
            double val = [valStr doubleValue]; 
            rightSetMax = MAX(val, rightSetMax);
            rightSetMin = MIN(val, rightSetMin);
            ChartDataEntry *entry = [[ChartDataEntry alloc] initWithX:j  y:val];
            [lineValls addObject:entry];
        }
        [alllineValues addObject:lineValls];
    }
    NSMutableArray *linedateSets = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < alllineValues.count; i++) {
        NSArray *lineVals = [alllineValues objectAtIndex:i];
        LineChartDataSet *lineSet = [[LineChartDataSet alloc] initWithValues:lineVals label:lineNames[i]];
        lineSet.lineWidth = 3.0f;
        lineSet.drawValuesEnabled = YES;
        lineSet.valueColors = @[[UIColor redColor]];
        [lineSet setColor:lineColors[i]];
        lineSet.drawCirclesEnabled = NO;
        lineSet.circleRadius = 3.0f;
        lineSet.drawCircleHoleEnabled = YES;
        lineSet.circleHoleRadius = 1.0f;
        lineSet.circleHoleColor = [UIColor redColor];
        lineSet.highlightEnabled = NO;
        lineSet.valueFont = [UIFont systemFontOfSize:12];
        lineSet.axisDependency = AxisDependencyRight;
        [linedateSets addObject:lineSet];
    }
    LineChartData *lineData = [[LineChartData alloc] initWithDataSets:linedateSets];
    
    //    柱状
    double leftSetMin = 0;
    double leftSetMax = 0;
    NSMutableArray *barsdateSetsArra = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < barValues.count; i++) {
        NSArray *barValue = [barValues objectAtIndex:i];
        NSMutableArray *yVals = [[NSMutableArray alloc] init];
        for (int j = 0; j < barValue.count ; j++)
        {
            NSString *valStr = [NSString stringWithFormat:@"%@", barValue[j]];
            double val = [valStr doubleValue];
            leftSetMax = MAX(val, leftSetMax);
            leftSetMin = MIN(val, leftSetMin);
            [yVals addObject:[[BarChartDataEntry alloc] initWithX:j  y:val]];
        }
        BarChartDataSet *barSet = [[BarChartDataSet alloc] initWithValues:yVals label:barNames[i]];
        [barSet setColor:barColors[i]];
        barSet.axisDependency = AxisDependencyLeft;
        barSet.drawValuesEnabled = NO;
        [barsdateSetsArra addObject:barSet];
    }
    BarChartData *barData = [[BarChartData alloc] initWithDataSets:barsdateSetsArra];
    barData.barWidth = barWidth;
    [barData setValueFont:[UIFont systemFontOfSize:10.f]];
    [barData groupBarsFromX: -0.5 groupSpace: groupSpace barSpace: barSpace];
    CombinedChartData *combineData = [[CombinedChartData alloc] init];
    
    double leftDiff = leftSetMax - leftSetMin;
    
    if (leftSetMax == 0.0 && leftSetMin == 0.0) {
        leftSetMax = 100.0;
        leftSetMin = 0.0;
    } else {
        leftSetMax = (leftSetMax + leftDiff * 0.1);
        leftSetMin = (leftSetMin - leftDiff * 0.1);
    }
    combineChartView1.leftAxis.axisMaximum = leftSetMax;
    combineChartView1.leftAxis.axisMinimum = 0;
    combineChartView1.leftAxis.labelCount = 10;
    double rightDiff = rightSetMax - rightSetMin;
    if (rightSetMax == 0 && rightSetMin == 0) {
        rightSetMax = 3.5;
        rightSetMin = 0.0;
    } else {
        rightSetMax = (rightSetMax + rightDiff * 0.1);
        rightSetMin = (rightSetMin - rightDiff * 0.1);
    }
    combineChartView1.rightAxis.axisMaximum = rightSetMax;
    combineChartView1.rightAxis.axisMinimum = rightSetMin;
//    combineChartView1.rightAxis.axisMinimum = -1.5;
    combineChartView1.rightAxis.labelCount = 10;
    combineChartView1.data = nil;
    combineData.lineData = lineData;
    combineData.barData = barData;
    ChartXAxis *xAxis = combineChartView1.xAxis;
    xAxis.labelPosition = XAxisLabelPositionBottom;
    xAxis.labelFont = [UIFont systemFontOfSize:10];
    xAxis.labelCount = xvalues.count;
    xAxis.drawLabelsEnabled = YES;
    xAxis.labelRotationAngle = -40;
    xAxis.yOffset = 10;
    xAxis.labelTextColor = ChartXAndYValueCorlo;
    xAxis.drawGridLinesEnabled = NO;
    xAxis.forceLabelsEnabled = NO;
    xAxis.valueFormatter = view;
    //    xAxis.gridAntialiasEnabled = YES;
    //    xAxis.axisMinimum = 5;
    xAxis.granularity = 1.0;
    //    xAxis.granularity = 0.5;
    
    //设置图例
    ChartLegend *legend = combineChartView1.legend;
    legend.enabled = NO;
//    legend.horizontalAlignment = ChartLegendHorizontalAlignmentLeft;
//    legend.verticalAlignment = ChartLegendVerticalAlignmentTop;
//    legend.orientation = ChartLegendOrientationHorizontal;
////    legend.drawInside = YES;
//    legend.direction = ChartLegendDirectionLeftToRight;
//    legend.form = ChartLegendFormCircle;
//    legend.formSize = 10;
//    legend.font = [UIFont systemFontOfSize:10];
//    legend.textColor = COLOR_HEX(0x595959);
//    legend.yOffset = 50;
    //左侧Y轴设置
    ChartYAxis *leftAxis = combineChartView1.leftAxis;
    leftAxis.labelPosition = YAxisLabelPositionOutsideChart;
    leftAxis.axisMinimum = 0.0f;
    leftAxis.drawAxisLineEnabled = NO;
    leftAxis.drawGridLinesEnabled = YES;
    leftAxis.labelTextColor = ChartXAndYValueCorlo;
    //右侧Y轴
    ChartYAxis *rightAxis = combineChartView1.rightAxis;
    rightAxis.labelPosition = YAxisLabelPositionOutsideChart;
    rightAxis.drawAxisLineEnabled = NO;
    rightAxis.drawGridLinesEnabled = NO;
    rightAxis.labelTextColor = ChartXAndYValueCorlo;
    if (showRight) {
        rightAxis.enabled = YES;
    }else{
        rightAxis.enabled = NO;
    }
    
    if (combineData.barData || combineData.lineData) {
        combineChartView1.data = combineData;
        //        combineChartView1.xAxis.axisMinimum = combineData.xMin - 0.25;
        //        combineChartView1.xAxis.axisMaximum = combineData.xMax + 0.25;
        combineChartView1.xAxis.axisMinimum = combineData.xMin - 0.5;
        combineChartView1.xAxis.axisMaximum = combineData.xMax + 0.5;
    }
    [combineChartView1 animateWithXAxisDuration:3.0 yAxisDuration:3.0];
    return view;
}





-(NSString *)stringForValue:(double)value axis:(ChartAxisBase *)axis{
    return self.xValusArray[(int)value];
}

@end
